package com.zhj.edu.entrypoint;

import com.zhj.edu.result.R;
import com.zhj.edu.result.ResponseCode;
import com.zhj.edu.utils.HttpResponseUtils;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 没有权限访问时执行的方法
 */
@Component
public class MyAuthenticationEntryPoint implements AuthenticationEntryPoint {
    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        R error = R.error(ResponseCode.NO_PERMISSION_EXCEPTION);
        //然后响应给前端
        HttpResponseUtils.sendResponse(response, error);
    }
}
